package com.piepesken.mediscale.viewmodels

import androidx.lifecycle.ViewModel
import com.huaweicloud.sdk.iotda.v5.IoTDAClient
import com.huaweicloud.sdk.iotda.v5.model.DeviceMessage
import com.huaweicloud.sdk.iotda.v5.model.ListDeviceMessagesRequest
import com.huaweicloud.sdk.iotda.v5.model.ListDevicesRequest
import com.huaweicloud.sdk.iotda.v5.model.QueryDeviceSimplify
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import javax.inject.Inject

@HiltViewModel
class ScaleViewModel @Inject constructor(
    private val api: IoTDAClient
) : ViewModel() {
    val devices: Flow<List<QueryDeviceSimplify>> = flow {
        val res = api.listDevices(ListDevicesRequest()).devices
        emit(res)
    }.flowOn(Dispatchers.IO)

    fun getDeviceMessages(deviceId: String): Flow<List<DeviceMessage>> = flow {
        val res = api.listDeviceMessages(ListDeviceMessagesRequest().withDeviceId(deviceId)).messages
        emit(res)
    }.flowOn(Dispatchers.IO)

//    val drugs: Flow<List<Drug>> by lazy {
//        drugRepo.getAllDrugs()
//    }
//
//    fun getDrugById(id: Int): Flow<Drug> {
//        return drugRepo.getDrugById(id)
//    }
//
//    fun filterDrugsByText(text: String): Flow<List<Drug>> {
//        return drugRepo.filterDrugsByText(text)
//    }
//
//    val isLoading: LiveData<Boolean> get() = _isLoading
}